<?php

class HabilitacionContableController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			//'accessControl', // perform access control for CRUD operations
			'rights',
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new HabilitacionContable;

		$model = $this->parseData($model);
		
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento)
	{
		$model=$this->loadModel($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		$model = $this->parseData($model);

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento)
	{
		if(Yii::app()->request->isPostRequest)
		{
			
			$this->loadModel($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento)->delete();
			
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$this->actionAdmin();
		/*
		$dataProvider=new CActiveDataProvider('HabilitacionContable');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
		*/
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new HabilitacionContable('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['HabilitacionContable']))
			$model->attributes=$_GET['HabilitacionContable'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($habilitacioncontable_persona_tipodocumento_id,$habilitacioncontable_persona_numerodocumento)
	{
		$criteria = new CDbCriteria();
		$criteria->condition = 'habilitacioncontable_persona_numerodocumento = :numDOC AND habilitacioncontable_persona_tipodocumento_id = :tipoDOC';
		$criteria->params = array(':numDOC'=>$habilitacioncontable_persona_numerodocumento,':tipoDOC'=>$habilitacioncontable_persona_tipodocumento_id);
		
		$model=HabilitacionContable::model()->find($criteria);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='habilitacion-contable-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	public function parseData(HabilitacionContable $model)
	{
		if(isset($_POST['HabilitacionContable']))
		{
			$criteria = new CDbCriteria();
			$criteria->condition = 'persona_numerodocumento = :numDOC AND persona_tipodocumento_id = :tipoDOC';
			$criteria->params = array(':numDOC'=>$_POST['Persona']['originalNumeroDoc'],':tipoDOC'=>$_POST['Persona']['originalTipoDoc']);
			$model->persona = Persona::model()->find($criteria);//FIXME se puede mejorar a lo mejor usardo findByPK
			//var_dump($model->persona);exit;
			if($model->persona!==null)
			{
				$model->habilitacioncontable_persona_numerodocumento =$_POST['Persona']['originalNumeroDoc'];
				$model->habilitacioncontable_persona_tipodocumento_id =$_POST['Persona']['originalTipoDoc'];
				$model->habilitacioncontable_matricula = $_POST['HabilitacionContable']['habilitacioncontable_matricula'];
				$model->habilitacioncontable_examen = $_POST['HabilitacionContable']['habilitacioncontable_examen'];
				$model->habilitacioncontable_inscripcion = $_POST['HabilitacionContable']['habilitacioncontable_inscripcion'];
				$model->habilitacioncontable_cursillo = $_POST['HabilitacionContable']['habilitacioncontable_cursillo'];
				$model->habilitacioncontable_estado_id = AplicacionVariables::ESTADO_ACTIVO_ID;
				if($model->save())
					$this->redirect(array('view','habilitacioncontable_persona_tipodocumento_id'=>$model->habilitacioncontable_persona_tipodocumento_id,'habilitacioncontable_persona_numerodocumento'=>$model->habilitacioncontable_persona_numerodocumento));
			}
		}
		return $model;
	}
}
